package com.huike.web.controller.review;


import com.github.pagehelper.Page;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/

    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable("name") String name, @PathVariable("age") Integer age, @PathVariable("sex") String sex){
        reviewService.saveByid(name,age,sex);
        return AjaxResult.success("成功插入:1条数据");
    }


    @PostMapping("/saveData")
    public AjaxResult saveData(@RequestBody Review review){
        reviewService.saveById(review);
        return AjaxResult.success("成功插入:1条数据");
    }
    /**=========================================================删除数据=============================================*/

    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        reviewService.removeById(id);
        return AjaxResult.success("删除成功");
    }
    /**=========================================================修改数据=============================================*/

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        reviewService.updateById(review);
        return AjaxResult.success("修改成功");
    }
    /**=========================================================查询数据=============================================*/

    /**
     * 根据id来查询数据
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review review  = reviewService.getById(id);
        return AjaxResult.success("操作成功",review);
    }





/*    @GetMapping("/getDataByPage/{pageNum}/{pageSize}")
    public AjaxResult getDataByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        Page<Review> reviewPage = new Page<>(pageNum,pageSize);
        reviewService.getDataByPage(reviewPage);
        return AjaxResult.success("查询成功",reviewPage);
    }*/



    /*@GetMapping("/getDataByPage/{pageNum}/{pageSize}")
    public AjaxResult getDataByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){*/
    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(Integer pageNum, Integer pageSize){
        startPage();
        List<Review> reviews  = reviewService.getPage(pageNum,pageSize);
        return getDataTable(reviews);

    }

}